#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
//回调函数1
void fun1()
{
    printf("callback fun1()...\n");
}
//回调函数2
void fun2()
{
    printf("callback fun2()...\n");
}
//主函数
int main()
{
    //声明两个进程id的变量
    pid_t k1, k2;
    //注册回调函数1
    if (atexit(fun1) != 0)
    {
        perror("register fun1 failed!");
        exit(0);
    }
    //注册回调函数2
    if (atexit(fun2) != 0)
    {
        perror("register fun2 failed!");
        exit(0);
    }
    //派生子进程1
    k1 = fork();
    if (k1 < 0) //子进程1派生失败
    {
        perror("fork k1 failled!");
        exit(1);
    }
    else if (k1 == 0) //子进程1派生结束
    {
        printf("child k1 is running!\n");
        printf("k1 : buffer data!\n");
        exit(0);
    }
    else
    {
        k2 = fork();
        if (k2 < 0)
        {
            perror("fork k2 failled!");
            exit(1);
        }
        else if (k2 == 0)
        {
            sleep(1);
            printf("child k2 is running!\n");
            printf("k2 : buffer data!\n");
            _exit(0);
        }
        else
        {
            sleep(2);
            printf("parent is running!\n");
            printf("parent : buffer data!\n");
            return 0;
        }
    }
    return 0;
}